<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class UserModel extends CI_Model {
	function __construct() {
		parent::__construct();
		$this -> load -> model("entities/userentity");
	}

	public function doLogin($uname, $upass) {
		$this->db->select('id, username, password, email');
		$query = $this -> db -> get_where('tbl_users', array('username' => $uname, 'password' => $upass, 'delete_flg' => 0));
		$user = $query -> first_row('UserEntity');
		if (count($user) == 1) {
			return $user -> getId();
		}
		return 0;
	}

	public function getUserInfo($userid) {
		$this->db->select('id, username, password, email');
		$query = $this -> db -> get_where('tbl_users', array('id' => $userid, 'delete_flg' => 0));
		$user = $query -> first_row('UserEntity');
		if (count($user) == 1) {
			return $user;
		}
		return null;
	}

	public function updateUserInfo($id, $newInfo) {
		$this -> db -> trans_begin();
		$this -> db -> where('id', $id);
		$this -> db -> update('tbl_users', $newInfo);
		if ($this -> db -> trans_status() === false) {
			$this -> db -> trans_rollback();
			return 0;
		} else {
			$this -> db -> trans_commit();
		}
		return 1;
	}

}
